/* rehash --- a decentralised hash<->hash store
   Copyright © 2020 Maxime Devos <maxime.devos@student.kuleuven.be>

   This file is part of rehash.

   rehash is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or (at
   your option) any later version.

   rehash is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with rehash.  If not, see <http://www.gnu.org/licenses/>. */

#include <gnunet/gnunet_config.h>
#include <stdint.h>
#include <string.h>
#include "rehash_types.h"
#include "rehash_util.h"
#include "fs_api.h"

static const int hash_lengths[] = {
  [REHASH_HASH_NONE] = 0,
  [REHASH_HASH_SHA256] = 32,
  [REHASH_HASH_SHA512] = 64,
  [REHASH_HASH_SHA3_256] = 32,
  [REHASH_HASH_SHA3_512] = 64,
  /* TODO check */
  [REHASH_HASH_BLAKE2S_256] = 32,
  /* */
  [REHASH_HASH_FS_REGULAR] = sizeof(struct FileIdentifier),
};

static const char hash_names[][12] = {
  [REHASH_HASH_SHA256]      = "sha256",
  [REHASH_HASH_SHA512]      = "sha512",
  [REHASH_HASH_SHA3_256]    = "sha3-256",
  [REHASH_HASH_SHA3_512]    = "sha3-512",
  [REHASH_HASH_BLAKE2S_256] = "blake2s-256",
  [REHASH_HASH_FS_REGULAR]  = "fs-regular",
};

int
REHASH_hashtype_strsize (enum GNUNET_REHASH_Hash_Type type)
{
  int index;
  index = type > REHASH_HASH_LAST ? 0 : (int) type;
  return hash_lengths[index];
}

int
REHASH_unparse_hashtype (enum GNUNET_REHASH_Hash_Type type, int size, char outbuf[])
{
  if ((type == REHASH_HASH_NONE) || (type > GNUNET_REHASH_HASH_LAST))
    return 0;
  strncpy(outbuf, hash_names[type], size);
  return size;
}

enum REHASH_Hash_Type
REHASH_parse_hashtype (const char *name)
{
  enum REHASH_Hash_Type candidate;
  for (candidate = 1; candidate <= REHASH_HASH_LAST; candidate++)
  {
    if (0 == strcmp(name, hash_names[(int) candidate]))
      return candidate;
  }
  return REHASH_HASH_NONE;
}
